package com.yyaccp.easybuy.security.config;

import com.yyaccp.easybuy.security.service.token.TokenService;
import com.yyaccp.easybuy.security.service.token.TokenServiceJwtImpl;
import com.yyaccp.easybuy.security.service.token.TokenServiceJwtRedisImpl;
import com.yyaccp.easybuy.security.service.token.TokenServiceRedisImpl;
import com.yyaccp.easybuy.security.vo.AuthProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 根据配置文件中auth.token-impl的值加载一个tokenService实现
 * redis - 用uuid为key, loginUser为value存储到redis
 * jwt - payload存储username,roles,perms,urls等
 * jwt-redis - 用uuid为key, loginUser为value存储到redis，payload存储uuid
 */
@Configuration
@EnableConfigurationProperties(AuthProperties.class)
public class TokenConfig {
    @Bean
    @ConditionalOnProperty(name = "auth.token-impl", havingValue = "redis", matchIfMissing = true)
    public TokenService redisTokenService() {
        return new TokenServiceRedisImpl();
    }

    @Bean
    @ConditionalOnProperty(name = "auth.token-impl", havingValue = "jwt")
    public TokenService jwtTokenService() {
        return new TokenServiceJwtImpl();
    }

    @Bean
    @ConditionalOnProperty(name = "auth.token-impl", havingValue = "jwt-redis")
    public TokenService jwtRedisTokenService() {
        return new TokenServiceJwtRedisImpl();
    }
}
